package gui;
import java.awt.BorderLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButtonMenuItem;
import javax.swing.JTextField;

import logic.Message;

import client.ClientConsole;

/**
 * Shows a librarian window, enables access for all librarian options 
 * including search.
 */
public class LibrarianWindow extends JPanel implements ActionListener{
	/**  GUI Panels used for Design*/
	private JPanel mainPanel,topPanel,searchPanel,optionsPanel,bottomPanel,searchTextPanel,radioPanel;
	private JTextField bookSearch;
	/** button representing all librarian action options*/
	private JButton search;
	private JButton addReader,inventoryManage,readerDetails,lendABook,returnBook;
	private JButton logout;
	private JLabel hello;
	/** radio Button for search selection*/
	JRadioButtonMenuItem  bookName,bookAuthor,freeText,subject;
	/** saves the properties of the GridBagLayout for*/
	GridBagConstraints gbc = new GridBagConstraints();
	/** Container for radio buttons*/  
	ButtonGroup bGroup = new ButtonGroup();
	
	/**Default constructor-librarian window*/
	public LibrarianWindow()
	{
		super();
		mainPanel=new JPanel();
		mainPanel.setLayout(new BorderLayout());
		bookSearch=new JTextField(20);
		search= new JButton("Search");
		topPanel=new JPanel();
		topPanel.setLayout(new BorderLayout());
		searchPanel=new JPanel();
		searchPanel.setLayout( new BoxLayout( searchPanel, BoxLayout.Y_AXIS ) );
		searchTextPanel=new JPanel();
		radioPanel= new JPanel();
		searchTextPanel.add(bookSearch);
		searchTextPanel.add(search);
		bookName=new JRadioButtonMenuItem ("Book name");
		subject=new JRadioButtonMenuItem ("Subject");
		bookAuthor=new JRadioButtonMenuItem ("Author");
		freeText=new JRadioButtonMenuItem ("Free Text");
		bGroup.add(bookName);
		bGroup.add(bookAuthor);
		bGroup.add(subject);
		bGroup.add(freeText);
		radioPanel.add(bookName);
		radioPanel.add(bookAuthor);
		bookName.setSelected(true);
		radioPanel.add(subject);
		radioPanel.add(freeText);
		searchPanel.add(searchTextPanel);
		searchPanel.add(radioPanel);
		search.addActionListener(this);
		hello=new JLabel("Hello "+ClientConsole.reader.getUsername()+",");
		topPanel.add(searchPanel,BorderLayout.CENTER);
		topPanel.add(hello,BorderLayout.WEST);
		mainPanel.add(topPanel,BorderLayout.NORTH);
		optionsPanel=new JPanel(new GridBagLayout());
		
		addReader=new JButton();
		addReader.addActionListener(this);
		addReader.setIcon(new ImageIcon("C:\\images\\user.png"));
		gbc.gridx=0;
		gbc.gridy=0;
		gbc.insets= new Insets(7,7,7,7);
		optionsPanel.add(addReader,gbc);
		inventoryManage=new JButton();
		inventoryManage.setIcon(new ImageIcon("C:\\images\\books.png"));
		gbc = new GridBagConstraints();
		gbc.gridx=1;
		gbc.gridy=0;
		gbc.insets= new Insets(7,7,7,7);
		optionsPanel.add(inventoryManage,gbc);
		inventoryManage.addActionListener(this);
		readerDetails=new JButton();
		readerDetails.setIcon(new ImageIcon("C:\\images\\viewuser.png"));
		gbc = new GridBagConstraints();
		gbc.gridx=2;
		gbc.gridy=0;
		gbc.insets= new Insets(7,7,7,7);
		optionsPanel.add(readerDetails,gbc);
		readerDetails.addActionListener(this);
		JPanel buttonOptions=new JPanel();
		lendABook=new JButton();
		lendABook.setIcon(new ImageIcon("C:\\images\\lendbook.png"));
		lendABook.addActionListener(this);
		returnBook=new JButton();
		returnBook.setIcon(new ImageIcon("C:\\images\\returnbook.png"));
		returnBook.addActionListener(this);
		buttonOptions.add(lendABook);
		buttonOptions.add(returnBook);
		gbc = new GridBagConstraints();
		gbc.gridx=0;
		gbc.gridy=1;
		gbc.gridwidth=3;
		gbc.insets= new Insets(7,7,7,7);
		optionsPanel.add(buttonOptions,gbc);
		
		mainPanel.add(optionsPanel,BorderLayout.CENTER);
		bottomPanel=new JPanel(new BorderLayout());
		logout=new JButton("Logout");
		logout.addActionListener(this);
		bottomPanel.add(logout,BorderLayout.WEST);
		mainPanel.add(bottomPanel,BorderLayout.SOUTH);
		add(mainPanel);
	}
	
	//Handles user's clicks on buttons
	@Override
	public void actionPerformed(ActionEvent ae) {
		if (ae.getSource()==addReader)
		{
			AddReaderForm ad=new AddReaderForm();
			this.setVisible(false);
			ClientConsole.aFrame.setContentPane(ad);
		}
		else if (ae.getSource()==returnBook)
		{
			ReturnBookForm rb=new ReturnBookForm();
			this.setVisible(false);
			ClientConsole.aFrame.setContentPane(rb);
		}
		else if (ae.getSource()==inventoryManage)
		{
			InventoryManagmentWindow imw=new InventoryManagmentWindow();
			this.setVisible(false);
			ClientConsole.aFrame.setContentPane(imw);
		}
		else if (ae.getSource()==lendABook)
		{
			LendBookForm lbf=new LendBookForm();
			this.setVisible(false);
			ClientConsole.aFrame.setContentPane(lbf);
		}
		if (ae.getSource()==readerDetails)
		{
			ViewReaderForm vrd=new ViewReaderForm();
			this.setVisible(false);
			ClientConsole.aFrame.setContentPane(vrd);
		}
		if (ae.getSource()==logout)
		{
			Message m;
			m=new Message("logout",ClientConsole.reader);
			ClientConsole.HandleMessageFromGui(m);	
			m=(Message)ClientConsole.client.getMessage();
			ClientConsole.reader=null;
			this.setVisible(false);
			ClientConsole.aFrame.setContentPane(new LoginForm());
			
		}
		if (ae.getSource()==search)
		{
			if (!bookSearch.getText().isEmpty())
			{
				String query=bookSearch.getText();
				String option = "";
				
				if (bookName.isSelected())
					option="Book Name";
				else if (bookAuthor.isSelected())
					option="Author";
				else if (subject.isSelected())
					option="Subject";
				else if (freeText.isSelected())
					option="Free";
				this.setVisible(false);
				ClientConsole.aFrame.setContentPane(new SearchForm(query,option));
			}
		}
		
	}
}
